package org.hgvc.webapp.ets.jsf.beans.agent;

import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;

import org.hgvc.webapp.ets.bo.Agent;
import org.hgvc.webapp.ets.bo.CommissionPaymentSchema;
import org.hgvc.webapp.ets.constant.AgentConstant;
import org.hgvc.webapp.ets.spring.service.AgentService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.FacesUtils;


public class AgentCommPaymentSchBean extends AbstractBean {

	public static final String BEAN_NAME="agentCommPaymentSchBean";
	public static final String NAV_LIST="agentCommPaySchList";
	public static final String NAV_CREATION="agentCommPaySchCreate";
	public static final String NAV_EDIT="agentCommPaySchEdit";
	private AgentService service;
	private CommissionPaymentSchema schema;
	private List<CommissionPaymentSchema> schemaList;
	private Agent agent;
	
	public CommissionPaymentSchema getSchema() {
		return schema;
	}
	public void setSchema(CommissionPaymentSchema schema) {
		this.schema = schema;
	}
	public void setService(AgentService service) {
		this.service = service;
	}
	public Agent getAgent() {
		return agent;
	}
	public void setAgent(Agent agent) {
		this.agent = agent;
	}
	/*---------------------------------------------------------------------------*/
	public List<CommissionPaymentSchema> getSchemaList() {
		try{
			schemaList = service.getCommissionPaymentSchemaListAgentId(agent.getAgentId());
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return schemaList;
	}
	
	public String navListAction(){
		if(agent==null){
			AgentBean bean = (AgentBean)FacesUtils.getManagedBean(AgentBean.BEAN_NAME);
			agent = bean.getAgent();
		}
		return NAV_LIST;
	}
	
	public String navCreateAction(){
		schema = new CommissionPaymentSchema();
		return NAV_CREATION;
	}
	
	public String createAction(){
		try{
			schema.setType(AgentConstant.AGENT_COMM_PAY_SCH_AGENT_TYPE);
			service.createCommissionPaymentSchemaAgent(schema, getThisUser(), agent);
			return NAV_LIST;
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return NAV_CREATION;
	}
	
	public void editActionListener(ActionEvent ae){
		UIParameter param = (UIParameter)ae.getComponent().findComponent("commId");
		String id = (String)param.getValue();
		for(CommissionPaymentSchema s:schemaList){
			if(s.getCommPaymentSchId().equalsIgnoreCase(id)){
				schema = s;
				break;
			}
		}
	}
	
	public String navEditAction(){
		return NAV_EDIT;
	}
	
	public String editAction(){
		try{
			service.editCommissionPaymentSchemaAgent(schema, getThisUser(), agent);
			return NAV_LIST;
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return NAV_EDIT;
	}
	
	public String deleteAction(){
		try{
			service.deleteCommissionPaymentSchema(schemaList, getThisUser());
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return NAV_LIST;
	}
	
}
